import axios from 'axios'
import { ElMessage } from 'element-plus'
import qs from 'qs'

const instance = axios.create({
  baseURL: import.meta.env.VITE_APP_URL,
  timeout: 60000,
  headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
})
const request = ({ url, method = 'GET', params = {} }) => {
  return new Promise((resolve, reject) => {
    method = method.toLowerCase();
    const requestBody = method === 'get' ? 'params' : 'data';
    const format = method === 'get' ? params : qs.stringify(params);
    const result = instance({ url, method, [requestBody]: format});
    result.then(value => resolve(value), reason => ElMessage.error(JSON.stringify(reason)));
  });
};

export const GOODSLIST = async params => await request({url: '/goods', params})

export const ADDGOODS = async params => await request({url: '/goods/add', method: "post", params})

export const EDITGOODS = async params => await request({url: '/goods/edit', method: "post", params})